package week5.day33;

import java.util.HashMap;

/**
 * @Author fengzihao
 * @Date 2024-05-17 18:45
 */
public class FindTheNewChar {
    public static void main(String[] args) {
        String srcString = "abcd";
        String newString = "baedc";
        char newChar = findNewChar(srcString, newString);
        System.out.println("newChar = " + newChar);
    }

    private static Character findNewChar(String srcString, String newString) {
        char[] srcChars = srcString.toCharArray();
        char[] newChars = newString.toCharArray();
        Character result = null;
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>();
        for (char srcChar : srcChars) {
            characterIntegerHashMap.put(srcChar, characterIntegerHashMap.getOrDefault(srcChar, 0) + 1);
        }
        for (char newChar : newChars) {
            characterIntegerHashMap.put(newChar, characterIntegerHashMap.getOrDefault(newChar, 0) - 1);
        }
        for (char newChar : newChars) {
            if (characterIntegerHashMap.get(newChar)<0) {
                result =  Character.valueOf(newChar);
            }
        }
        return result;
    }
}
